/*
 * This calendar application is brought to you
 * by Group 3 of User Interface Programming II, vt11.
 *
 * Group Members:
 * Pooya | Flora | Monica | Can
 */
package vical.controller;

import java.util.ArrayList;
import java.util.Calendar;
import vical.data.DataManager;
import vical.model.Entry;
import vical.model.Internationalization;
import vical.undo.*;

/**
 * This is the controller for entries. All the public method should be static.
 *
 * @author Pooya
 * @author Can
 */
public class EntryController {

    private static UndoManager undoManager = new UndoManager();

    private EntryController() {
    }

    /**
     * Loads all entries
     * @return
     */
    public static ArrayList<Entry> LoadEntries() {
        return DataManager.getInstance().getAllEntries();
    }

    /**
     * Loads all entries of a specific date/time
     * @return
     */
    public static ArrayList<Entry> LoadEntries(Calendar startTime) {
        return DataManager.getInstance().getAllEntries(startTime);
    }

    /**
     * Gets all categories
     * @return
     */
    public static ArrayList<String> getCategories() {
        ArrayList<String> categories = new ArrayList<String>();
        categories.add(Internationalization.getString("category_work"));
        categories.add(Internationalization.getString("category_play"));
        ArrayList<Entry> allEntries = EntryController.LoadEntries();
        for (Entry entry : allEntries) {
            if (!categories.contains(entry.getCategory())) {
                categories.add(entry.getCategory());
            }
        }
        return categories;
    }

    /**
     * Gets all properties
     * @return
     */
    public static ArrayList<String> getPriorities() {
        ArrayList<String> priorities = new ArrayList<String>();
        ArrayList<Entry> allEntries = EntryController.LoadEntries();
        for (Entry entry : allEntries) {
            if (!priorities.contains(entry.getPriority())) {
                priorities.add(entry.getPriority());
            }
        }
        return priorities;
    }

    // return 0 if success
    public static int addEntry(Entry newEntry) {
        return addEntry(newEntry, true);
    }
    // return 0 if success

    public static int addEntry(Entry newEntry, boolean registerUndo) {
        if (registerUndo) {
            undoManager.registerEvent(new UndoableEntryAddEvent(newEntry));
        }
        DataManager.getInstance().addEntry(newEntry);
        return -1;
    }

    // return 0 if success
    public static int updateEntry(Entry oldEntry, Entry newEntry) {
        undoManager.registerEvent(new UndoableEntryUpdateEvent(oldEntry, newEntry));
        return updateEntry(newEntry);
    }

    // return 0 if success
    public static int updateEntry(Entry newEntry) {
        DataManager.getInstance().editEntry(newEntry);
        return -1;
    }

    // return 0 if success
    public static int deleteEntry(Entry entry) {
        return deleteEntry(entry, true);
    }
    // return 0 if success

    public static int deleteEntry(Entry entry, boolean registerUndo) {
        if (registerUndo) {
            undoManager.registerEvent(new UndoableEntryDeleteEvent(entry));
        }
        DataManager.getInstance().deleteEntry(entry);
        return -1;
    }

    public static UndoManager getUndoManager() {
        return undoManager;
    }
}
